export function throttle<T extends (...args: unknown[]) => unknown>(
  func: T,
  delay = 200,
): (...args: Parameters<T>) => void {
  let lastCall = 0;
  let timeoutId: ReturnType<typeof setTimeout> | null = null;
  let lastArgs: Parameters<T>;

  return (...args: Parameters<T>) => {
    const now = Date.now();
    lastArgs = args;

    if (now - lastCall >= delay) {
      if (timeoutId) {
        clearTimeout(timeoutId);
        timeoutId = null;
      }
      lastCall = now;
      func(...args);
    } else if (!timeoutId) {
      timeoutId = setTimeout(
        () => {
          lastCall = Date.now();
          func(...lastArgs);
          timeoutId = null;
        },
        delay - (now - lastCall),
      );
    }
  };
}

// TODO src\utils\tools.ts

// export function debounce<T extends (...args: unknown[]) => unknown>(
//   func: T,
//   delay = 200
// ): (...args: Parameters<T>) => void {
//   let timer: NodeJS.Timeout | null = null;
//   return (...args: Parameters<T>) => {
//     if (timer) {
//       clearTimeout(timer);
//     }
//     timer = setTimeout(() => {
//       func(...args);
//     }, delay);
//   };
// }
